package com.archerdu.apply.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.archerdu.apply.model.Examin;
import com.archerdu.apply.model.User;
import com.archerdu.apply.service.ExaminManager;
import com.archerdu.base.controller.BaseController;
import com.archerdu.base.dao.IPage;
import com.archerdu.commons.constant.DataConstant;
import com.archerdu.commons.helper.examin.ExaminHelper;
import com.archerdu.commons.helper.examin.ScoreInfo;

@Controller
@Scope(value = "prototype")
@RequestMapping(value = "/admin/examin")
public class ExaminController extends BaseController {

    private static final long serialVersionUID = -8730779611525448972L;

    private ExaminManager     examinManager;
    private Examin            examin;

    /**
     * 添加试卷
     * 
     * @param requ
     * @param ec
     * @return
     */
    @RequestMapping(value = "/add")
    public ModelAndView add(HttpServletRequest requ, ExaminController ec) {
        ModelAndView mav = new ModelAndView("");
        examin = ec.getExamin();
        if ("post".equalsIgnoreCase(requ.getMethod())) {
            try {
                examin.setCreatetime(getSqlDate());
                examin.setCreateuser(((User) requ.getSession().getAttribute(DataConstant.SESSION_SYS_USER)).getId());
                examinManager.add(examin);
                addMessage(DataConstant.COMMON_ADD_SUCCESS);
            } catch (Exception e) {
                addMessage(DataConstant.COMMON_ADD_FAILED);
                logger.error(e.getMessage(), e);
            }
        }
        mav.setViewName("admin/examin/add");
        return mav;
    }

    /**
     * 修改试卷
     * 
     * @param requ
     * @param ec
     * @return
     */
    @RequestMapping(value = "/edit")
    public ModelAndView edit(HttpServletRequest requ, ExaminController ec) {
        ModelAndView mav = new ModelAndView("");

        if ("post".equalsIgnoreCase(requ.getMethod())) {
            try {
                examin = ec.getExamin();
                examin.setUpdatetime(getSqlDate());
                examin.setUpdateuser(((User) requ.getSession().getAttribute(DataConstant.SESSION_SYS_USER)).getId());
                examinManager.modify(examin);
                addMessage(DataConstant.COMMON_EDIT_SUCCESS);
            } catch (Exception e) {
                addMessage(DataConstant.COMMON_EDIT_FAILED);
                logger.error(e.getMessage(), e);
            }
        } else if (ec.getExamin() != null && ec.getExamin().getId() != null) {
            examin = examinManager.get(ec.getExamin().getId());
        } else {
            examin = new Examin();
        }
        
        // 读取分数级别信息
        if (null != examin.getScoreSection()) {
            List<ScoreInfo> scoreInfoList = ExaminHelper.toList(examin.getScoreSection());
            mav.addObject("scoreInfoList", scoreInfoList);
        }
        
        mav.addObject(examin);
        mav.setViewName("admin/examin/edit");
        return mav;
    }

    /**
     * 删除试卷，未实现，考虑使用use_yn实现删除效果
     * 
     * @param requ
     * @return
     */
    @RequestMapping(value = "/delete")
    public ModelAndView delete(HttpServletRequest requ) {
        ModelAndView mav = new ModelAndView("");
        // TODO 试卷下有题目，或者已经有答卷不能删除试卷
        return mav;
    }

    /**
     * 分页查询试卷
     * 
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/page")
    public ModelAndView page(ExaminController ec, Integer pageNo, Integer pageSize) {
        ModelAndView mav = new ModelAndView("");
        examin = ec.getExamin();
        if (examin == null) {
            examin = new Examin();
        }
        if (null == pageNo || null == pageSize) {
            pageNo = 1;
            pageSize = 20;
        }
        IPage<Examin> page = examinManager.pageSearch(examin, pageNo, pageSize);
        mav.addObject("page", page);
        mav.setViewName("admin/examin/page");
        return mav;
    }

    public ExaminManager getExaminManager() {
        return examinManager;
    }

    public void setExaminManager(ExaminManager examinManager) {
        this.examinManager = examinManager;
    }

    public Examin getExamin() {
        return examin;
    }

    public void setExamin(Examin examin) {
        this.examin = examin;
    }
}
